package com.kingyea.mobilepolice.apimanage.providerservicemonitor.service.impl;

import com.kingyea.mobilepolice.apimanage.providerservicemonitor.bean.ProviderServiceMonitor;
import com.kingyea.mobilepolice.apimanage.providerservicemonitor.dao.ProviderServiceMonitorMapper;
import com.kingyea.mobilepolice.apimanage.providerservicemonitor.service.ProviderServiceMonitorService;
import com.kingyea.mobilepolice.apimanage.utils.DateUtils;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description ProviderServiceMonitor 实现类
 * @date Created in 2018-05-21 14:11:27
 */
@Service
@Transactional
public class ProviderServiceMonitorServiceImpl implements ProviderServiceMonitorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProviderServiceMonitorServiceImpl.class);

    @Resource
    private ProviderServiceMonitorMapper providerServiceMonitorMapper;

    public static void main(String[] args) throws Exception {
        ProviderServiceMonitorServiceImpl p = new ProviderServiceMonitorServiceImpl();
        System.out.println(p.generateTimeId(new Date()));
        String dateStr = "2018/05/22";
        Date date = DateUtils.string2Date(dateStr);
        System.out.println(date);
        System.out.println(p.generateTimeId(date));

    }

    /**
     * 通过ID查找对象
     *
     * @param hostSerialNo
     * @param timeId
     * @return
     */
    @Override
    public ProviderServiceMonitor getById(String hostSerialNo, Long timeId) throws Exception {
        return this.providerServiceMonitorMapper.getById(hostSerialNo, timeId);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<ProviderServiceMonitor> list(ProviderServiceMonitor providerServiceMonitor) throws Exception {
        List<ProviderServiceMonitor> list = providerServiceMonitorMapper.list(providerServiceMonitor);
        return list;
    }

    /**
     * 删除
     *
     * @param hostSerialNo, timeId
     * @return
     */
    @Override
    public int deleteById(String hostSerialNo, Long timeId) throws Exception {
        providerServiceMonitorMapper.deleteById(hostSerialNo, timeId);
        return 1;
    }

    /**
     * 保存
     *
     * @param providerServiceMonitor
     * @return
     */
    @Override
    public ProviderServiceMonitor save(ProviderServiceMonitor providerServiceMonitor) throws Exception {
        try {
            if (StringUtils.isEmpty(providerServiceMonitor.getHostSerialNo())) {
                throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
            }
            if (StringUtils.isEmpty(providerServiceMonitor.getTimeId())) {
                providerServiceMonitor.setTimeId(generateTimeId(new Date()));
            }
            ProviderServiceMonitor existObj = getById(providerServiceMonitor.getHostSerialNo(), providerServiceMonitor.getTimeId());
            if (existObj != null) {// 检查是否存在
                throw new BusinessException(StateCode.UNIQUE_IDENTIFIER_EXIST);
            }
            providerServiceMonitorMapper.save(providerServiceMonitor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return providerServiceMonitor;
    }

    private Long generateTimeId(Date date) {// 最多每分钟上报一次，所以返回yyyyMMddHHmm
        String YYYYMMDDHHMMSS = DateUtils.date2String(date, DateUtils.YYYYMMDDHHMMSS);
        String YYYYMMDDHHMM = YYYYMMDDHHMMSS.substring(0, YYYYMMDDHHMMSS.length() - 2);
        return Long.valueOf(YYYYMMDDHHMM);
    }

    /**
     * 更新
     *
     * @param providerServiceMonitor
     * @return
     */
    @Override
    public ProviderServiceMonitor update(ProviderServiceMonitor providerServiceMonitor) throws Exception {
        try {
            providerServiceMonitorMapper.update(providerServiceMonitor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return providerServiceMonitor;
    }

    @Override
    public ProviderServiceMonitor report(ProviderServiceMonitor providerServiceMonitor) throws Exception {
        return save(providerServiceMonitor);
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        if (map.containsKey("startTime") && !StringUtils.isEmpty(map.get("startTime"))) {
            map.put("startTime", generateTimeId((Date) map.get("startTime")));
        }
        if (map.containsKey("endTime") && !StringUtils.isEmpty(map.get("endTime"))) {
            map.put("endTime", generateTimeId((Date) map.get("endTime")));
        }
        return PageInfo.getPageInfo(providerServiceMonitorMapper.searchByCondition(map));
    }

}
